package com.cp.admin.modular.system.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.cp.admin.api.entity.vo.DeptVO;
import com.cp.admin.core.common.constant.factory.ConstantFactory;
import com.cp.admin.modular.system.model.Dept;
import com.cp.core.node.ZTreeNode;
import com.cp.admin.modular.system.service.IDeptService;
import com.cp.admin.modular.system.dao.DeptMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements IDeptService {

    @Resource
    private DeptMapper deptMapper;
//    @Resource
//    private IUserService userService;

    @Override
    public void deleteDept(Long deptId) {
        Dept dept = deptMapper.selectById(deptId);

        Wrapper<Dept> wrapper = new EntityWrapper<>();
        wrapper = wrapper.like("pids", "%[" + dept.getId() + "]%");
        List<Dept> subDepts = deptMapper.selectList(wrapper);
        for (Dept temp : subDepts) {
            temp.deleteById();
        }

        dept.deleteById();
    }

    @Override
    public List<ZTreeNode> tree() {
        return this.baseMapper.tree();
    }

    @Override
    public List<Map<String, Object>> list(String condition) {
        return this.baseMapper.list(condition);
    }

    @Override
    public List<Dept> getChildDeptList(Long pId) {
        Wrapper<Dept> wrapper = new EntityWrapper<>();
        wrapper.eq("pid",pId);
        return this.selectList(wrapper);
    }

    @Override
    public DeptVO getDeptList(Long deptId) {
        DeptVO deptVO = null;
        if (deptId == null || deptId == 0){
            deptVO = new DeptVO();
            deptVO.setId(0L);
            deptVO.setFullname("公司");
            deptVO.setSubDept(getChildDeptVO(0L));
        }else {
            Dept dept = this.selectById(deptId);
            if(dept != null){
                deptVO = new DeptVO();
                BeanUtils.copyProperties(dept,deptVO);
                //获取子部门信息
                deptVO.setSubDept(getChildDeptVO(dept.getId()));
            }
        }
        return deptVO;
    }

//    @Override
//    public List<UserListVO> getDeptUser(Long deptId) {
//        List<UserListVO> userListVOS = null;
//        List<Long> deptIds = new ArrayList<>();
//        if (deptId == null || deptId == 0){
//            deptId = 0L;
//            //获取子部门Id集合
//            List<Long> subDeptIds = ConstantFactory.me().getSubDeptId(deptId);
//            deptIds.addAll(subDeptIds);
//        }else {
//            //部门ID
//            deptIds.add(deptId);
//            //获取子部门Id集合
//            List<Long> subDeptIds = ConstantFactory.me().getSubDeptId(deptId);
//            deptIds.addAll(subDeptIds);
//        }
//        if(CollectionUtils.isNotEmpty(deptIds)){
//            //获取所有部门下的员工
//            Wrapper<User> wrapper = new EntityWrapper<>();
//            wrapper.eq("status",1);//启用的员工
//            wrapper.in("deptid",deptIds);
//            List<User> userList = userService.selectList(wrapper);
//            if(CollectionUtils.isNotEmpty(userList)){
//                userListVOS = new ArrayList<>();
//                for(User user:userList){
//                    UserListVO userListVO = new UserListVO();
//                    userListVO.setUserId(user.getId().longValue());
//                    userListVO.setUserName(user.getName());
//                    userListVOS.add(userListVO);
//                }
//            }
//        }
//        return userListVOS;
//    }

    @Override
    public List<Long> getDeptChildList(Long deptId) {
        List<Long> subDeptIds = ConstantFactory.me().getSubDeptId(deptId);
        subDeptIds.add(deptId);
        return subDeptIds;
    }

    /**
     * 递归获取子部门的DeptVO集合
     * @param pId
     * @return
     */
    private List<DeptVO> getChildDeptVO(Long pId){
        List<DeptVO> deptVOList = null;
        List<Dept> deptList = getChildDeptList(pId);
        if(CollectionUtils.isNotEmpty(deptList)){
            deptVOList = new ArrayList<>();
            for(Dept dept:deptList){
                DeptVO deptVO = new DeptVO();
                BeanUtils.copyProperties(dept,deptVO);
                //递归获取子部门
                deptVO.setSubDept(getChildDeptVO(deptVO.getId()));
                deptVOList.add(deptVO);
            }
        }
        return deptVOList;
    }
}
